/*
 * @Author: Your name
 * @Date:   2020-06-27 09:00:51
 * @Last Modified by:   Your name
 * @Last Modified time: 2020-07-21 23:51:22
 */
import Mock, { mock } from "mockjs";
console.log("mockjs导入成功");

/*
    获取首页商品数据  包含三个（流行/新款/精选）
    接口地址：/api/goods/?type=pop&pageindex=1
    传入参数:
        类型：pop/new/sell
        分页的页码 pageIndex
*/
const { popGoodsList } = Mock.mock({
  "popGoodsList|90": [
    {
      "id|+1": 1,
      title: "流行男友约会美女百搭",
      image:
        "https://s5.mogucdn.com/mlcdn/c45406/180719_58lb3ij7i3dlc7ebf9i4gj48i7k90_640x960.jpg_440x587.v1cAC.40.webp",
      "star|0-1000": 0,
      "price|25-900.1-2": 0
    }
  ]
});

const { newGoodsList } = Mock.mock({
  "newGoodsList|90": [
    {
      "id|+1": 1,
      title: "新款男友约会美女百搭",
      image:
        "https://s5.mogucdn.com/mlcdn/c45406/191019_1kjc5b0d9ak2367e7c81gke3ki4jc_640x960.jpg_440x587.v1cAC.40.webp",
      "star|0-1000": 0,
      "price|25-900.1-2": 0
    }
  ]
});

const { sellGoodsList } = Mock.mock({
  "sellGoodsList|90": [
    {
      "id|+1": 1,
      title: "精选男友约会美女百搭",
      image:
        "https://s5.mogucdn.com/mlcdn/55cf19/190623_7h44aig3901ghbie077kd6l6b3c44_640x960.jpg_440x587.v1cAC.40.webp",
      "star|0-1000": 0,
      "price|25-900.1-2": 0
    }
  ]
});
//  /api/goods/?type=pop&pageindex=1
Mock.mock(/\/api\/goods/, "get", options => {
  let getListByPage = function(goodsList){
    let pageIndex = getQuery(options.url,"pageindex");
    let pageSize = 10;
    let start = (pageIndex-1) * pageSize;
    let end = pageIndex * pageSize;
    let pageTotle = Math.ceil(goodsList.length/pageSize);
    const list = pageIndex>pageTotle?[]:goodsList.slice(start,end);
    // console.log(list);
    return list;
    
  } 
  let type = getQuery(options.url, "type");
  // console.log('解析到的type值：',type);
  if (type === "pop") {
    var resultList = getListByPage(popGoodsList);  
  } else if (type === "new") {
    var resultList = getListByPage(newGoodsList);  
  } else if (type === "sell") {
    var resultList = getListByPage(sellGoodsList);
  }
  return {
      status:0,
      message:resultList
  }
});

/* 
  获取商品详情页信息
  请求方式：get
  API地址：/api/detail/:id
  传入参数：id
  url地址写法：/api/detail/50
*/
const {detailData} = Mock.mock({
  "detailData|90": [
    {
      "id|+1": 1,
      title: "夏季新款网红冰丝针织衫短袖条纹T恤女ins宽松短款露脐上衣潮",
      topImages: [
        "https://s11.mogucdn.com/mlcdn/c45406/200210_23298b1018ag0kk8il1g452eh8dc0_640x960.jpg_640x960.v1cAC.70.webp",
        "https://s5.mogucdn.com/mlcdn/c45406/200210_1e458g870h2ace81702dhgalgjch9_640x960.jpg_640x960.v1cAC.70.webp",
        "https://s5.mogucdn.com/mlcdn/c45406/200210_769kidh8l4a39bi2268d3l59d273d_640x960.jpg_640x960.v1cAC.70.webp",
        "https://s5.mogucdn.com/mlcdn/c45406/200210_1d88e9i9908fla16bibf0d5c9b71c_640x960.jpg_640x960.v1cAC.70.webp"
      ],
      "newPrice|25-900.1-2": 0,
      "oldPrice|300-1000.1-2": 0,
      sellCounts: 1000,
      discount: "七折",
      store: 100,
      service: [
        "延误必赔", "退货补运费", "全国包邮", "七天无理由退货"
      ],
      promise: "72小时发货",
      shopInfos: {
        logo: "https://s5.mogucdn.com/mlcdn/c45406/190515_3defc7eak01l8eag9idkbeaa22bjd_200x200.jpg_120x120.webp",
        shopName: "女神de衣橱",
        "totleSell|8000-1000000": 0,
        "baobeiCounts|10-100": 0,
        score:[
          {name: "描述相符", "isDesc|1-9.1-2": 0, isBetter: true},
          {name: "价格何理", "isDesc|1-9.1-2": 0, isBetter: false},
          {name: "质量满意", "isDesc|1-9.1-2": 0, isBetter: true}
        ]
      },
      detailImages:[
        {
          name: "穿着样式",
          images: [
            "https://s11.mogucdn.com/mlcdn/c45406/200619_7hha4gge5ded329cl6fjddkg2l9la_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_2636a8j7e60dhj1a49kdj4bcb1l36_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_05lj4llaa3hf9ha5ak1d6caidegbk_800x800.jpg_468x468.webp",
            "https://s11.mogucdn.com/mlcdn/c45406/200619_0l61hh31229jci680l46gg7k7g986_800x800.jpg_468x468.webp",
            "https://s11.mogucdn.com/mlcdn/c45406/200619_71h51h4319b6342d621ieg0lbc6ag_800x800.jpg_468x468.webp",
            "https://s11.mogucdn.com/mlcdn/c45406/200619_5d233919g7100b595708d26fdf773_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_1k2ed5eh1fga8j6245e7b9gj3a221_800x800.jpg_468x468.webp"
          ]
        },
        {
          name: "其他搭配",
          images: [
            "https://s11.mogucdn.com/mlcdn/c45406/200619_02929fl251dfj5jlcaj0k2bjke5f5_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_4f18a6661b3gj7lac4baacc594dg2_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_15fj4ad0i6i5l043eck18508lhiif_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_43797b0i8jhj4696llhlba8gh37dh_800x800.jpg_468x468.webp",
            "https://s5.mogucdn.com/mlcdn/c45406/200619_014lecla1dd8ibgha238ha7f6j03d_800x800.jpg_468x468.webp"
          ]
        }
      ],
      rules:{
        disclaimer: "**以上尺寸均为实物人工测量，因测量当时不同会有1-2cm的误差，相关数据仅供参考，以收到实物为准",
        key: "尺码说明",
        table: [
          [
            ["尺码", "M", "S", "XL", "L"],
            ["腰围", "68", "64", "76", "72"],
            ["臀围", "94", "90", "102", "98"],
            ["裤长", "88", "87", "90", "89"],
            ["裤口", "31", "30", "33", "32"],
            ["档长", "", "", "", "", ],
            ["大腿围", "", "", "", "", ]
          ]
        ],
        paramsTable: [
          [
            ["图案", "花色"],
            ["裙型", "不规则"],
            ["颜色", "黑色，粉色，灰色"],
            ["袖型", "常规袖"],
            ["衣门襟", "套头"],
            ["摆型", "不对称"],
            ["腰型", "高腰"],
            ["尺码", "S,XL,L,M"],
            ["版型", "紧身"],
            ["季节", "夏季"],
            ["材质", "其他"],
            ["组合形式", "单件"],
            ["领型", "V领"]
          ]
        ]
      }
    }
  ]
});

Mock.mock(/\/api\/detail/, 'get', (options) => {
  let newDetailObj = {};
  let id = getQuery(options.url, "id");
  for(var index in detailData){
    if(parseInt(id) === detailData[index].id){
      newDetailObj = detailData[index];
    }
  }
  return {
    "status": 0,
    "message": newDetailObj
  }
});

/* 
  获取详情页评论信息
  请求方式：get
  API地址：/api/comment/:id
  传入参数：id
  url地址写法：/api/comment/50
*/
const {commentData} = Mock.mock({
  "commentData|90": [
    {
      "id|+1": 1,
      commentMes: [
        {
          "userImage": "https://s5.mogucdn.com/mlcdn/5abf39/180522_8g0aff41cf49b81d1di45l864d0gl_132x132.jpg_48x48.webp",
          "userName": "小傻妞儿Rose",
          content: "料子舒服，颜色正，款式喜欢，价格也不贵，包装仔细",
          params: "XL黄色",
          date: '@date(yyyy-MM-dd)'
        },
        {
          userImage: "https://s5.mogucdn.com/p1/160105/idid_ifrtcnbzgbswgntfguzdambqhayde_160x160.jpg",
          userName: "语***4",
          content: "款式好看” “没有线头” “没有气味” “包装高大上” “性价比高” “卖家服务很好” “上身效果好” “做工满意",
          params: "XXL黑色",
          date: '@date(yyyy-MM-dd)'
        },
        {
          userImage: "https://s5.mogucdn.com/mlcdn/c45406/191219_6dllcbjb5ea698f2jkj676fffj26b_400x400.jpg",
          userName: "谢谢谢谢谢谢大婶i",
          content: "也就那样吧 凑合着穿",
          params: "L粉色",
          date: '@date(yyyy-MM-dd)'
        },
        {
          userImage: "https://s5.mogucdn.com/mlcdn/5abf39/180522_8g0aff41cf49b81d1di45l864d0gl_132x132.jpg_48x48.webp",
          userName: "小傻妞儿Rose",
          content: "料子舒服，颜色正，款式喜欢，价格也不贵，包装仔细",
          params: "L玫红",
          date: '@date(yyyy-MM-dd)'
        }
      ]
      
    }
  ]
});
Mock.mock(/\/api\/comment/, 'get', (options) => {
  let newComment = {};
  let id = getQuery(options.url, "id");
  for(var index in commentData){
    if(parseInt(id) === commentData[index].id){
      newComment = commentData[index];
    }
  }
  return {
    status: 0,
    "message": newComment
  }
})

/* 
  获取商品推荐数据
  请求方式：get
  API地址：/api/recommend
  传入参数：无
  url地址写法：/api/recommend
*/
const {recommendData} = Mock.mock({
  "recommendData|20": [
    {
      "id|+1": 1,
      title: "夏季2020新款网红镂空链条设计感显瘦短款露脐短袖T恤女上衣",
      image:
        "https://s5.mogucdn.com/mlcdn/c45406/190722_35cbbe2aj3lighk19hjc11j4bi1ed_800x1200.jpg_320x428.v1cAC.40.webp",
      "star|0-1000": 0,
      "price|25-900.1-2": 0
    }
  ]
});

Mock.mock('/api/recommend', 'get', () =>{
  return {
    status: 0,
    message: recommendData
  }
})
//解析url地址
var getQuery = function(url, name) {
  let index = url.indexOf("?");
  if (index != -1) {
    let urlQueryArr = url.substr(index + 1).split("&");
    for (let i = 0; i < urlQueryArr.length; i++) {
      let itemArr = urlQueryArr[i].split("=");
      if (itemArr[0] == name) {
        return itemArr[1];
      }
    }
  }
  return null;
};
